package com.cpi.cornpp.versionsvalues.action;

import java.util.Locale;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.apache.struts.Globals;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.actions.MappingDispatchAction;
import org.apache.struts.util.MessageResources;

import com.cpi.cornpp.common.exception.CornPPException;
import com.cpi.cornpp.versionsvalues.exception.VersionsValuesException;
import com.cpi.cornpp.versionsvalues.form.VersionsValuesForm;
import com.cpi.cornpp.versionsvalues.services.VersionsValuesService;
import com.cs.common.utils.NumberUtil;
/**
 * Versions Values Action
 * @author Edgar Joao
 *
 */
public class VersionsValuesAction extends MappingDispatchAction{
	/** Default forward */
	private final static String DEFAULT_FORWARD = "default";//$NON-NLS-1$
	/** Custom exception */
	private final static String CUSTOM_EXCEPTION = "custom_exception";//$NON-NLS-1$
	/** Id de version */
	private final static String VERSION_ID_LABEL = "versionId";//$NON-NLS-1$
	/** Save success */
	private final static String SAVE_SUCCESS = "label.purchases.instructions.save.success";//$NON-NLS-1$
	/** Information message name*/
	private final static String MESSAGE = "MESSAGE";//$NON-NLS-1$
	/**
	 * Show Versions Values
	 * @param mapping
	 * @param form
	 * @param request
	 * @param response
	 * @return
	 * @throws Exception
	 */
	public ActionForward goToVersionsValues(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
			throws Exception {		
		VersionsValuesService service = null;
		VersionsValuesForm versionsValuesForm = null;			
		HttpSession httpSession = request.getSession(true);	
		int versionId = 0;
		try{
			service = new VersionsValuesService();
			versionsValuesForm = (VersionsValuesForm) form;	
			versionId = NumberUtil.getInt(httpSession.getAttribute(VERSION_ID_LABEL));
			versionsValuesForm.setVersionsValuesList(service.getVersionsValuesList(versionId));						
		}catch (VersionsValuesException versionsValuesException){
			httpSession.setAttribute(CUSTOM_EXCEPTION, versionsValuesException);
			throw versionsValuesException;
		}catch (Exception exception){
			VersionsValuesException versionsValuesException = null;
			exception.printStackTrace(System.out);
			versionsValuesException= new VersionsValuesException(exception, CornPPException.LAYER_ACTION,CornPPException.ACTION_LISTS);
			httpSession.setAttribute(CUSTOM_EXCEPTION, versionsValuesException);
			throw versionsValuesException;
		}
		return mapping.findForward(DEFAULT_FORWARD);
	}
	/**
	 * Save Versions Values
	 * @param mapping
	 * @param form
	 * @param request
	 * @param response
	 * @return
	 * @throws Exception
	 */
	public ActionForward save(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
			throws Exception {
		VersionsValuesService service = null;
		VersionsValuesForm versionsValuesForm = null;	
		HttpSession httpSession = request.getSession(true);
		String messageError = null;	
		MessageResources messageResources = null;
		int versionId = 0;
		try{
			service = new VersionsValuesService();
			versionsValuesForm = (VersionsValuesForm) form;
			messageResources = getResources(request);
			versionId = NumberUtil.getInt(httpSession.getAttribute(VERSION_ID_LABEL));
			service.save(versionId, versionsValuesForm.getVersionsValuesList());
			versionsValuesForm.setVersionsValuesList(service.getVersionsValuesList(versionId));			
			messageError = messageResources.getMessage((Locale)httpSession.getAttribute(Globals.LOCALE_KEY), SAVE_SUCCESS);
			httpSession.setAttribute(MESSAGE, messageError);			
		}catch (VersionsValuesException versionsValuesException){
			httpSession.setAttribute(CUSTOM_EXCEPTION, versionsValuesException);
			throw versionsValuesException;
		}catch (Exception exception){
			VersionsValuesException versionsValuesException = null;
			exception.printStackTrace(System.out);
			versionsValuesException= new VersionsValuesException(exception, CornPPException.LAYER_ACTION,CornPPException.ACTION_LISTS);
			httpSession.setAttribute(CUSTOM_EXCEPTION, versionsValuesException);
			throw versionsValuesException;
		}
		versionsValuesForm.reset(mapping, request);
		form.reset(mapping, request);
		return mapping.findForward(DEFAULT_FORWARD);
	}
}
